package com.bkitmobile.kesu.appengine.web.client;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.bkitmobile.kesu.appengine.web.client.Listener.LogOutListener;
import com.bkitmobile.kesu.appengine.web.client.SupportPopup.ConfigureAccountPopup;
import com.bkitmobile.kesu.appengine.web.client.SupportPopup.FeePayingPopup;
import com.bkitmobile.kesu.appengine.web.client.SupportPopup.GoogleMap;
import com.bkitmobile.kesu.appengine.web.client.SupportPopup.HostInformation;
import com.bkitmobile.kesu.appengine.web.client.SupportPopup.LocationInfoPopup;
import com.bkitmobile.kesu.appengine.web.client.SupportPopup.LoginPopup;
import com.bkitmobile.kesu.appengine.web.client.SupportPopup.RegisterPopup;
import com.bkitmobile.kesu.appengine.web.client.entity.ListOfPlace;
import com.bkitmobile.kesu.appengine.web.client.entity.PlaceClient;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class KeSuServer implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	final String login_string_id = "login_btn";
	final String register_string_id = "register_btn";
	ToggleButton register_btn ;
	
	private boolean haveClickMap = false;
	private GoogleMap map = new GoogleMap();;
	private JavaScriptObject mapJS = null;
	private final HostInformation hostPage = new HostInformation();
	private final StackPanel panel = new StackPanel();
	private final Tree information = new Tree();
	private final Tree map_tree = new Tree();
	private final Tree account = new Tree();
	private String userName = "";
	ToggleButton login_btn;
	RootPanel login_pan = RootPanel.get(this.login_string_id);
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	
	public final void setNormalActionButton (final ToggleButton t)
	{
		t.addMouseOverHandler(new MouseOverHandler() {
			
			@Override
			public void onMouseOver(MouseOverEvent event) {
				// TODO Auto-generated method stub
				 t.setDown(false);
			}
		});
		t.addMouseOutHandler(new MouseOutHandler() {
			
			@Override
			public void onMouseOut(MouseOutEvent event) {
				// TODO Auto-generated method stub
				t.setDown(true);
			}
		});
		t.addMouseDownHandler(new MouseDownHandler() {
			
			@Override
			public void onMouseDown(MouseDownEvent event) {
				// TODO Auto-generated method stub
				t.setDown(true);
			}
		});
		
		t.addMouseUpHandler(new MouseUpHandler() {
			
			@Override
			public void onMouseUp(MouseUpEvent event) {
				// TODO Auto-generated method stub
				t.setDown(false);
			}
		});
		
		
	}


	/////1 la add thong tin trang chu
	/////2 la add thong tin lien he
	/////3 la......
	/////4 la add ban do ko co su kien click chuot
	////5 la add ban do co su kien click chuot
	private final void addInfoTab (int choice)
	{
		RootPanel.get("info").clear();
		switch (choice)
		{
		case 1:
			RootPanel.get("info").add(this.hostPage);
			break;
		case 4:
			RootPanel.get("info").add(map);
			if (this.haveClickMap)
			{
				this.haveClickMap = false;
				map.deactivateClickMap();
			}
			if (mapJS == null)
			{
				mapJS = map.initMap(map.getId());
				map.setMap(mapJS);
			}
			break;
		case 5:
			RootPanel.get("info").add(map);
			if (!this.haveClickMap)
			{
				this.haveClickMap = true;
				map.activateClickMap();
			}
			if (mapJS == null)
			{
				mapJS = map.initMap(map.getId());
				map.setMap(mapJS);
			}
			break;
		}
	}
	
	/////Tao button 
	@SuppressWarnings("deprecation")
	public void onModuleLoad() {

		///// served for the change of style sheet in click event
		final ArrayList<Label> label_index = new ArrayList<Label>();
		///// add StackPanel vao khung tab
		RootPanel tab_pan = RootPanel.get("tab");
		panel.setWidth("170px");
		tab_pan.add(panel);
		
		
		///// add cac tree vao trong Stack Panel
		panel.add(information,"Trang ch\u1ee7",false);
		panel.add(this.map_tree,"B\u1ea3n \u0111\u1ed3",false);
		
		
		final Label information_tree1 = new Label ("Th\u00f4ng tin trang ch\u1ee7");
		label_index.add(information_tree1);
		information_tree1.addClickListener(new ClickListener() {
			@Override
			public void onClick(Widget sender) {
				// TODO Auto-generated method stub
				for (Label i:label_index)
				{
					i.removeStyleName("highlight");
				}
				information_tree1.addStyleName("highlight");
				addInfoTab(1);
			}
		});
		
		final Label map_tree1 = new Label("B\u1ea3n \u0111\u1ed3 Vi\u1ec7t Nam");
		label_index.add(map_tree1);
		map_tree1.addClickListener(new ClickListener() {
			
			@Override
			public void onClick(Widget sender) {
				// TODO Auto-generated method stub
				for (Label i:label_index)
				{
					i.removeStyleName("highlight");
				}
				map_tree1.addStyleName("highlight");
				////// Add ban do vao khung info
				addInfoTab(4);
			}
		});
		
		final Label map_tree2 = new Label("T\u1ea1o \u0111\u1ecba \u0111i\u1ec3m");
		label_index.add(map_tree2);
		map_tree2.addClickListener(new ClickListener() {
			
			@Override
			public void onClick(Widget sender) {
				// TODO Auto-generated method stub
				for (Label i:label_index)
				{
					i.removeStyleName("highlight");
				}
				map_tree2.addStyleName("highlight");
				////// Add ban do vao khung info
				
				Window.alert("B\u1ea1n h\u00e3y click ch\u1ecdn" +
						" \u0111\u1ecba \u0111i\u1ec3m m\u00e0 b\u1ea1n mu\u1ed1n th\u00eam v\u00e0o");
				addInfoTab(5);
			}
		});
		
		////// add tung label vao trong information
		
		information.addItem(information_tree1);
		
		/////add tung labal vao trong map_tree
		map_tree.addItem(map_tree1);
		
		
		
		/////Kiem tra xem co login hay chua de quyet dinh gia tri cua tab btn
		greetingService.checkLogin("", new AsyncCallback<String>() 
		{
			/////tao button dang nhap va dang ky
			///// khi chua login
			/////////////////
			///////////////
			public void create2Button()
			{
			//// add button Dang nhap
				login_btn = new ToggleButton();
				login_btn.setText("\u0110\u0103ng nh\u1eadp");
				login_btn.setPixelSize(66, 15);
				login_btn.setDown(true);
				
				//////add button Dang ky
				register_btn= new ToggleButton();
				register_btn.setText("\u0110\u0103ng k\u00fd");
				register_btn.setPixelSize(50, 15);
				register_btn.setDown(true);
				
				RootPanel register_pan = RootPanel.get("register_btn");
				register_pan.clear();
				register_pan.add(register_btn);
				
				RootPanel login_pan = RootPanel.get("login_btn");
				login_pan.clear();
				login_pan.add(login_btn);
				login_btn.addClickListener(new ClickListener() 
				{
					
					@Override
					public void onClick(final Widget sender) {
						// TODO Auto-generated method stub
						final LoginPopup log_pop_up = new LoginPopup();
						//register_btn.setVisible(false);
						//register_btn.setEnabled(false);
						//login_btn.setEnabled(false);
						//log_pop_up.setStyleName("myLogIn");
						//RootPanel.get("theme").setStyleName("hideTheme");
					    //RootPanel.get().setStyleName(false);
						log_pop_up.show();
						
					}
				});
				
				setNormalActionButton(login_btn);
				
				register_btn.addClickListener(new ClickListener() {
					
					@Override
					public void onClick(Widget sender) {
						// TODO Auto-generated method stub
						RegisterPopup reg_pop_up = new RegisterPopup();
						//reg_pop_up.setStyleName("myRegister");
						//RootPanel.get("theme").setStyleName("hideTheme");
					    //RootPanel.get().setStyleName(false);
						reg_pop_up.show();
					}
				});
				setNormalActionButton(register_btn);
			}
			
			public void create1Button(String usrName)
			{
				RootPanel login_pannel = RootPanel.get("login_btn");
				RootPanel register_pannel = RootPanel.get("register_btn");
				Label usr = new Label();
				usr.setText("Xin ch\u00e0o " + usrName);
				login_pannel.add(usr);
				final ToggleButton logout_btn = new ToggleButton();
				logout_btn.setText("\u0110\u0103ng xu\u1ea5t");
				logout_btn.setPixelSize(66, 15);
				logout_btn.setDown(true);
				register_pannel.add(logout_btn);
				logout_btn.addClickListener(new LogOutListener());
				setNormalActionButton(logout_btn);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				if (result.equals(""))
				{	
					addInfoTab(1);
					this.create2Button();
				}
				else
				{
					final Label account1 = new Label ("Th\u00f4ng tin t\u00e0i kho\u1ea3n");
					label_index.add(information_tree1);
					account1.addClickListener(new ClickListener() {
						@Override
						public void onClick(Widget sender) {
							// TODO Auto-generated method stub
							ConfigureAccountPopup tmp = new ConfigureAccountPopup();
							tmp.show();
						}
					});
					final Label map_tree3 = new Label("Mua \u0111\u1ecba \u0111i\u1ec3m");
					map_tree3.addClickListener(new ClickListener() {
						
						@Override
						public void onClick(Widget sender) {
							// TODO Auto-generated method stub
							FeePayingPopup tmp = new FeePayingPopup();
							tmp.show();
						}
					});
					panel.add(account, "Th\u00f4ng tin t\u00e0i kho\u1ea3n",false);
					account.addItem(account1);
					
					
					addInfoTab(4);
					map_tree.addItem(map_tree2);
					map_tree.addItem(map_tree3);
					this.create1Button(result);
					userName = result;
					greetingService.getPlace("", new AsyncCallback<ListOfPlace>() {
						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}
						@Override
						public void onSuccess(ListOfPlace result) {
							// TODO Auto-generated method stub
							List <PlaceClient> insert = result.getPlaces();
							//System.out.println("Cac dia diem");
							for (PlaceClient i:insert)
							{
								map.addMarker(i.getLatitude(), i.getLongtitude(), i.getType(), map.isPrivate(i.getType()),
										i.getName(),i.getType(),i.getInfo(),i.getAddress());
								//System.out.println(i.getType() + "\t" + i.getLatitude() + "\t" + i.getLongtitude());
							}
						}
					});
				}
			}
		});
	}
}